import Time from '@/common/time'
var fieldType = {
  base: {
    name: ''
  },
  state: {
    render(field) {
      var map = this.parseRangeset(field)
      return (v) => {
        return map[v] || v
      }
    },
    parseRangeset(field) {
      let stateMap = {}
      if (field.rangeset) {
        if (typeof field.rangeset === 'string') {
          let list
          if (field.rangeset.indexOf('@') < 0 && field.rangeset.indexOf(';') >= 0) {
            list = field.rangeset.split(';')
          } else {
            list = field.rangeset.split('@')
          }
          list.forEach((str) => {
            let item = str.split(':')
            stateMap[item[0]] = item[1]
          })
        } else if (typeof field.rangeset === 'object') {
          stateMap = field.rangeset
        }
      }
      return stateMap
    },
    parseRangesetList(field) {
      let stateList = []
      if (field.rangeset) {
        if (typeof field.rangeset === 'string') {
          let list
          if (field.rangeset.indexOf('@') < 0 && field.rangeset.indexOf(';') >= 0) {
            list = field.rangeset.split(';')
          } else {
            list = field.rangeset.split('@')
          }
          list.forEach((str) => {
            let item = str.split(':')
            stateList.push(item[0])
          })
        }
      }
      return stateList
    }
  },
  bool: {
    render() {
      return (v) => {
        if (v) {
          return '<i class="ivu-icon ivu-icon-checkmark-circled text-success"></i>'
        } else {
          return '<i class="ivu-icon ivu-icon-minus-circled text-danger"></i>'
        }
      }
    }
  },
  date: {
    render() {
      return (v) => {
        return Time.toDate(v)
      }
    }
  },
  time: {
    render() {
      return (v) => {
        return Time.toTime(v)
      }
    }
  },
  timestamp: {
    render() {
      return (v) => {
        if (v) {
          return `<div class="data-time">${v && Time.toTime(v)}</div>`
        } else {
          return null
        }
      }
    }
  },
  expCondition: {
    fullRow: true
  },
  memo: {
    fullRow: true
  },
  exp_condition: {
    fullRow: true
  },
  money: {
    regExp: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  },
  integer: {
    regExp: /^\d+$/
  },
  number: {
    regExp: /^(-?\d+)(\.\d+)?$/
  }
}
fieldType.stat = fieldType.state

export default fieldType
